package com.simple.business.service.evaluation.impl;

import com.alibaba.fastjson2.JSONObject;
import com.github.zuihou.base.R;
import com.simple.business.domain.enumeration.EvaluationTypeEnum;
import com.simple.business.domain.vo.evaluation.EvaluationQuestionVO;
import com.simple.business.service.evaluation.ToyService;
import com.simple.business.util.LLMUtil;
import com.simple.business.util.RuleHandleUtil;
import com.simple.llm.domain.constant.LLMPromptConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @desc
 * @Author Simple
 * @Date 2025/3/10 22:17
 **/
@Slf4j
@Service
public class ToyServiceImpl implements ToyService {

    @Resource
    private LLMUtil llmUtil;

    @Override
    public R<List<EvaluationQuestionVO>> createEvaluationQuestion(String evaluationType) {

        String evaluationTypeName = EvaluationTypeEnum.getDescByCode(evaluationType);

        String origin = llmUtil.chatWithSingleMsg(
                "你是一个测评儿童" + evaluationTypeName + "的助手，你的任务是提供专业、准确的测评问题。",
                LLMPromptConstant.EVALUATION_QUESTION_SISASSISTANT_INIT
                        .replaceAll("\\{evaluationTypeName}",evaluationTypeName),
                "好的，请提供测评问题的类型。",
                "测评问题类型：'''" + evaluationTypeName + "'''",
                "测评", "origin");

        try {
            //直接转json可能异常，特殊处理
            JSONObject jsonObject = RuleHandleUtil.handleFormat(origin);
            if (Objects.isNull(jsonObject)) {
                log.error("大模型生成面试问题异常: " + origin);
                return R.fail("网络繁忙，请稍后重试");
            }

            List<EvaluationQuestionVO> list = IntStream.range(1, 6).mapToObj(i -> new EvaluationQuestionVO(
                    jsonObject.getString("q" + i),
                    jsonObject.getString("ra" + i)
                    //createVoiceFile(aiQuestionsDTO, jsonObject.getString("q" + i), i)
                    )
            ).collect(Collectors.toList());

            return R.success(list);
        } catch (Exception exception) {
            log.info("生成问题失败，大模型返回内容：" + origin);
            return R.fail("生成面试题目异常");
        }
    }


}
